package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.entity.CmsNotice;
import com.yuke.cloud.service.cmc.service.CmsNoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2019/09/27.
*/
@Api(value = "WEB - CmsNoticeController", tags = "处理CmsNotice相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/notice")
public class CmsNoticeController extends BaseController {
    @Resource
    private CmsNoticeService cmsNoticeService;

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsNotice", value = "该数据记录的POJO") @RequestBody CmsNotice cmsNotice) {
        cmsNoticeService.save(cmsNotice);
        return WrapMapper.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsNoticeService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsNotice", value = "需要修改的内容") @RequestBody CmsNotice cmsNotice) {
        cmsNoticeService.update(cmsNotice);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsNotice> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsNotice cmsNotice = cmsNoticeService.findById(id);
        return WrapMapper.ok(cmsNotice);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsNotice>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CmsNotice> list = cmsNoticeService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/information")
    @ApiOperation(httpMethod = "POST", value = "获取通知栏信息")
    public Wrapper<List<CmsNotice>> getInformation(@ApiParam(name = "cmsNotice", value = "获取通知信息") @RequestBody CmsNotice cmsNotice ){

        return WrapMapper.ok(cmsNoticeService.getInformation(cmsNotice));
    }

    @GetMapping("/modify/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsNotice> modify(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsNotice notice = cmsNoticeService.selectByKey(id);
        Integer nStatus = (int)notice.getnStatus();
        if (nStatus+1>3){
            return WrapMapper.error("已经发送3次！系统禁止发送操作！");
        }
        CmsNotice cmsNotice = cmsNoticeService.modifyInformation(notice);
        return WrapMapper.ok(cmsNotice);
    }

    @GetMapping("/new")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取最新具体信息")
    public  Wrapper<CmsNotice> newModify() {

        return WrapMapper.ok(cmsNoticeService.getNewNotice());
    }

}
